<?php
/**
 * @license    GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the CommunalMeals Component
 *
 */

class CommunalMealsViewSignUpReport extends JView
{
	function display($tpl = null)
	{
		global $option, $mainframe;
		
		// Get ids from request
		$groupId = JRequest::getVar('group_id', NULL );
		$eventId = JRequest::getVar('event_id', NULL ); // If null, display all events of the group
		$tpl = JRequest::getVar('tpl', NULL );
		
		// Get data from model
		$eventGroup = CMeals::readEventGroup($groupId);
		if (is_null($eventId)) {
			$eventList = CMeals::readEventList($groupId);
			CMealsEvent::appendSummarizedEventStatus($eventGroup);
			$isCrewMember = CMeals::isEventGroupCrew(CMealsFrontEndUtils::getCurrentMemberId(), $groupId);
		}
		else {
			$eventList = array( CMeals::readEvent($eventId, $groupId) );
			$isCrewMember = CMeals::isEventCrew(CMealsFrontEndUtils::getCurrentMemberId(), $groupId, $eventId);
		}
		CMealsSignup::appendSignupData($eventList);
		$memberList = CMeals::readMemberList();
		
		// Menu
		$menu = new CMealsMenu();
		$menu->addLink("SHOW_LIST", $this->baseurl."/index.php?option=".$option."&view=eventgroups");
		
		// Pass data to template
		$this->assignRef( 'eventId', $eventId );
		$this->assignRef( 'eventList', $eventList );
		$this->assignRef( 'eventGroup', $eventGroup );
		$this->assignRef( 'memberList', $memberList );
		$this->assignRef( 'isCrewMember', $isCrewMember );
        $this->assignRef( 'cmealsMenu', $menu );
		$this->assignRef( 'option', $option );
		
		// Select appropriate subtemplate
		// (there may already be a request variable $tpl)
		// null => Sign up report for one or all events in group
		// eventform => Sign up report for one event with option to close event
		// excel => Sign up report for all events with option to set billing and create/open excel report
		// excel => Also applied if all events have archive status (i.e. no signup data available)
		if (is_null($tpl)) {
			if (!is_null($eventId) && $isCrewMember && $eventList[0]->status == CMeals::STATUS_OPEN) {
				$tpl = "eventform";
			}
		}
		else if ($tpl == "eventform") {
			if (is_null($eventId)) $tpl = null; // No event manipulation for multiple events
			else if ($eventList[0]->status != CMeals::STATUS_OPEN) $tpl = null; // No need for form if event not open
			else if (!$isCrewMember) $tpl = null; // Form is only for crew members
		}
		else if ($tpl == "excel") {
			if (!is_null($eventId)) $tpl = null; // No excel report for single events
			if (!$eventGroup->allEventsClosed) $tpl = null; // Excel report only if all events closed
		}
		if ($eventGroup->allEventsArchive) {
			$tpl = "excel"; // Force excel report, since no signup data will be available
		}
		
		parent::display($tpl);
	}
	
}